package com.eudi.ssm.petstore.backend.web.controller;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.eudi.ssm.petstore.backend.model.User;
import com.eudi.ssm.petstore.backend.service.user.IUserService;
import com.eudi.ssm.petstore.web.model.PagingList;
import com.eudi.ssm.petstore.web.model.SearchModel;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value="/list")
	public String list(Model model, @ModelAttribute("searchModel") SearchModel formModel, 
			@RequestParam(value=PagingList.PAGE_INDEX_NAME, defaultValue="1") int pageIndex,
			@RequestParam(value=PagingList.PAGE_SIZE_NAME, defaultValue="10") int pageSize) {

		int id = 0;
		String name = "";
		if(formModel != null) {
			id = NumberUtils.toInt(formModel.getId(), 0);
			name = formModel.getName();
		}
		model.addAttribute("searchModel", formModel);
		PagingList<User> userModel = this.userService.findUsers(id, name, pageIndex, pageSize);
		model.addAttribute("userModel", userModel);

		return "user/list";
	}
}
